package com.meilai.project.service.business.offer.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.entity.business.combo.GasStationCombo;
import com.meilai.project.entity.business.offer.Offer;
import com.meilai.project.entity.system.Dictionary;
import com.meilai.project.mapper.business.offer.OfferMapper;
import com.meilai.project.service.business.building.BuildingAreaService;
import com.meilai.project.service.business.combo.GasStationComboService;
import com.meilai.project.service.business.offer.OfferService;
import com.meilai.project.service.system.DictionaryService;
import com.meilai.project.service.system.MediaTypeSpecPriceService;
import com.meilai.project.vo.business.media.MediaTypeSpecPriceVO;
import com.meilai.project.vo.business.media.MediaTypeVO;
import com.meilai.project.vo.business.offer.OfferQueryVO;
import com.meilai.project.vo.business.offer.OfferVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 报价表 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-01-11
 */
@Service
public class OfferImpl extends ServiceImpl<OfferMapper, Offer> implements OfferService {
    @Autowired
    private GasStationComboService gasStationComboService;

    @Autowired
    private BuildingAreaService buildingAreaService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private MediaTypeSpecPriceService mediaTypeSpecPriceService;

    @Override
    public List<Map<String, Object>> page(Long industry_id) {
        List<Dictionary> dictionaries = dictionaryService.listByCode("khxg-hy");
        if (industry_id != null) {
            dictionaries = dictionaries.stream().filter(item -> item.getId().equals(industry_id)).collect(Collectors.toList());
        }
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<OfferVO> resultList = baseMapper.page(null);
        List<MediaTypeSpecPriceVO> mediaTypeSpecPriceList = mediaTypeSpecPriceService.allList();
        Map<Long, List<OfferVO>> industryMap = resultList.stream().collect(Collectors.groupingBy(OfferVO::getIndustry_id));
        for (int i = 0; i < dictionaries.size(); i++) {
            Map<String, Object> map = new HashMap<>();//key 为id加name value 为对象
            map.put("industry_id", dictionaries.get(i).getId());
            map.put("industry_name", dictionaries.get(i).getName());
            if (industryMap.get(dictionaries.get(i).getId()) != null) {
                List<OfferVO> offerList = industryMap.get(dictionaries.get(i).getId());
                for (MediaTypeSpecPriceVO mediaTypeSpecPriceVO : mediaTypeSpecPriceList) {
                    List<OfferVO> collect = offerList.stream().filter(item -> item.getMedia_type_spec_price_id().equals(mediaTypeSpecPriceVO.getId())).collect(Collectors.toList());
                    if (collect.size() > 0) {
                        map.put(mediaTypeSpecPriceVO.getId().toString(), collect.get(0));
                    } else {
                        OfferVO vo = new OfferVO();
                        BeanUtils.copyProperties(mediaTypeSpecPriceVO, vo);
                        map.put(mediaTypeSpecPriceVO.getId().toString(), vo);
                    }
                }
            } else {
                for (MediaTypeSpecPriceVO mediaTypeSpecPriceVO : mediaTypeSpecPriceList) {
                    OfferVO vo = new OfferVO();
                    BeanUtils.copyProperties(mediaTypeSpecPriceVO, vo);
                    map.put(mediaTypeSpecPriceVO.getId().toString(), vo);
                }
            }
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public List<Map<String, String>> getMediaList() {
        List<Map<String, String>> mapArrayList = new ArrayList<>();
        List<GasStationCombo> list = gasStationComboService.list(Wrappers.<GasStationCombo>lambdaQuery().isNull(GasStationCombo::getDeleted_at));
        for (GasStationCombo item : list) {
            Map<String, String> map = new HashMap<>();
            map.put("id", item.getId().toString());
            map.put("name", item.getName());
            map.put("is_media", "0");
            mapArrayList.add(map);
        }
        List<MediaTypeVO> mediaTypes = buildingAreaService.getMediaTypes();
        List<MediaTypeVO> collect = mediaTypes.stream().filter(item -> !item.getId().equals(5L) && !item.getId().equals(6L) && !item.getId().equals(7L)).collect(Collectors.toList());
        for (MediaTypeVO vo : collect) {
            Map<String, String> map = new HashMap<>();
            map.put("id", vo.getId().toString());
            map.put("name", vo.getName());
            map.put("is_media", "1");
            mapArrayList.add(map);
        }
        return mapArrayList;
    }

    @Override
    public List<OfferVO> getOfferList(Long mediaTypeId, Integer is_media) {
        return baseMapper.getOfferList(mediaTypeId, is_media);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(Offer offer) {
        return null;
    }

    @Override
    public Boolean settingsRedLinePrice(Long industry_id, Long media_type_spec_id, BigDecimal red_line_price) {
        if (this.count(Wrappers.<Offer>lambdaQuery().eq(Offer::getIndustry_id, industry_id).eq(Offer::getMedia_type_spec_price_id, media_type_spec_id)) > 0) {
            return this.update(Wrappers.<Offer>lambdaUpdate().set(Offer::getRed_line_price, red_line_price).eq(Offer::getIndustry_id, industry_id).eq(Offer::getMedia_type_spec_price_id, media_type_spec_id));
        } else {
            Offer offer = new Offer();
            offer.setDiscount(null);
            offer.setRed_line_price(red_line_price);
            offer.setMedia_type_spec_price_id(media_type_spec_id);
            offer.setIndustry_id(industry_id);
            return this.save(offer);
        }
    }

    @Override
    public List<OfferQueryVO> queryByIndustryId(Long industry_id) {
        List<OfferQueryVO> offerQueryVOS = baseMapper.queryByIndustryId(industry_id);
        List<Long> collect = offerQueryVOS.stream().map(OfferQueryVO::getMedia_type_spec_price_id).collect(Collectors.toList());
        offerQueryVOS.addAll(mediaTypeSpecPriceService.allList().stream().filter(item -> !collect.contains(item.getId())).map(item -> {
            OfferQueryVO offerQueryVO = new OfferQueryVO();
            offerQueryVO.setMedia_type_spec_price_id(item.getId());
            offerQueryVO.setMedia_spec_name(item.getMedia_type_spec_name());
            offerQueryVO.setPrice_day(item.getPrice_day());
            offerQueryVO.setPrice_month(item.getPrice_month());
            offerQueryVO.setPrice_week(item.getPrice_week());
            return offerQueryVO;
        }).collect(Collectors.toList()));
        return offerQueryVOS;
    }
}